package com.hctforgreen.greenservice.utils;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;

/**
 * 调用系统裁剪，
 * 解决了三星手机（无法保存裁剪后的图像）问题
 * @author Tallen Zhai
 *
 */
public class ImageCropUtil {
	
	private String tmpFilePath;
	public ImageCropUtil(){
		
		tmpFilePath = HctConstants.DEFAULT_ROOT_PATH + 
				HctConstants.DEFAULT_CACHE_STORE_PATH +
				"crop";
		/**
		 * 三星手机如果没有事先创建出这个文件夹，保存不了裁剪后的图片
		 */
		FolderOper.createSDCardDir(HctConstants.DEFAULT_CACHE_STORE_PATH);
	}
	public String getTmpFilePath(){
		return tmpFilePath;
	}
	private Uri getTmpFileUri(){
		return Uri.parse("file://" + tmpFilePath);
	}
	public void doCropImage(Activity activity, int requestCode) {
		/**
		 * Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
		 */
		Intent intent = new Intent(Intent.ACTION_PICK);
		
		intent.setType("image/*");
		
		intent.putExtra("crop", "true");
		
//		intent.putExtra("aspectX", 1);// 裁剪框比例
//		
//		intent.putExtra("aspectY", 1);
//		
//		intent.putExtra("outputX", 1280);// 输出图片大小
//		
//		intent.putExtra("outputY", 1280);
		
		intent.putExtra("scale", true);
		
		intent.putExtra("return-data", false);
		
		intent.putExtra(MediaStore.EXTRA_OUTPUT, getTmpFileUri());
		
		intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
		
		intent.putExtra("noFaceDetection", true); // no face detection
		activity.startActivityForResult(intent, requestCode);
	}
	/**
	 * 
	 * @param albumUri file://前缀的SDCard地址
	 * @param activity
	 * @param requestCode
	 */
	public void doCropImage(Uri albumUri, Activity activity, int requestCode) {

		Intent intent = new Intent("com.android.camera.action.CROP");
		
		intent.setDataAndType(albumUri, "image/*");

		intent.putExtra("crop", "true");

//		intent.putExtra("aspectX", 2);
//
//		intent.putExtra("aspectY", 1);
////
//		intent.putExtra("outputX", 400);
//
//		intent.putExtra("outputY", 200);

		intent.putExtra("scale", true);

		intent.putExtra(MediaStore.EXTRA_OUTPUT, getTmpFileUri());

		intent.putExtra("return-data", false);

		intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());

		intent.putExtra("noFaceDetection", true); // no face detection

		activity.startActivityForResult(intent, requestCode);
	}
}
